package com.yuncheng.spcyApi.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;


import java.util.List;

import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;

/**
 * 自定义排序功能
 * @author hua
 */
public class OrderItemUtils {

    // 是否进行驼峰转下划线
    private static final boolean isToUnder = true;

    /**
     * 排序组合
     * @param queryWrapper {@link LambdaQueryWrapper}
     * @param column       排序字段
     * @param orderType    排序类型
     * @param <T>          泛型
     */
    private static <T> void setOrderOne(LambdaQueryWrapper<T> queryWrapper, String column, String orderType) {

        String col = isToUnder ? StrUtil.toUnderlineCase(column) : column;
        try {
            ISqlSegment[] sqlSegments = {ORDER_BY, () -> col, orderType.equals("asc") ? ASC : DESC};
            queryWrapper.getExpression().add(sqlSegments);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 当个自定义排序
     * @param queryWrapper {@link LambdaQueryWrapper}
     * @param orderItem    {@link OrderItemVo} 排序
     * @param <T>          泛型
     */
    public static <T> void setOrderItem(LambdaQueryWrapper<T> queryWrapper, OrderItemVo orderItem) {
        setOrderOne(queryWrapper, orderItem.getOrderItem(), orderItem.getOrderType());
    }

    /**
     * 多个自定义排序
     * @param queryWrapper {@link LambdaQueryWrapper}
     * @param list         {@link OrderItemVo} 排序
     * @param <T>          泛型
     */
    public static <T> void setOrderItem(LambdaQueryWrapper<T> queryWrapper, List<OrderItemVo> list) {
        for (OrderItemVo order : list) {
            setOrderItem(queryWrapper, order);
        }
    }

}
